
#include "core.h"
#include "http_server.h"
#include "http_request.h"
#include "http_response.h"
#include "mylog.h"

#include <string.h>
#include <signal.h>
#include <stdio.h>

struct core* c = NULL;
static int  askTime = 0;
void onSigInt(int sig) {
	mylog(LOG_INFO, "onSigInt");
	if (c) {
		mylog(LOG_INFO, "=========");
		stopCore(c);
	}
}

void httpCallBack(struct HttpRequest* request, struct HttpResponse* response) {
	mylog(LOG_INFO, "client ==== on httpCallBack!!!");
	setHttpStatusCodeHttpResponse(response, HttpStatus_200OK);
	char* statusMessage = "OK";
	int statusMessageLen = strlen(statusMessage);
	setSatusMessageHttpResponse(response, statusMessage, statusMessageLen);
	char testContent[10240];
	int i;
	for (i = 0; i < 10240 - 1; i++) {
		testContent[i] = 'a';
	}
	testContent[10240 -1] = 0;

	char body[102400];
	int bodyLen = snprintf(body, sizeof(body), "%s\r\n HELLO WORLD!!!, times[%d]", testContent, ++askTime);
	setBodyHttpResponse(response, body, bodyLen);
}

int main() {
	signal(SIGINT, onSigInt);
	int workThreadNum = 4;
	int serverNum = 1;
	c = initCore(workThreadNum, serverNum);
	if (!c) {
		return -1;
	}

	char *bindaddr = "127.0.0.1";
	int port = 1300;
	struct httpServer* hServer = initHttpServer(c, NULL, port, bindaddr);
	if (!hServer) {
		destroyCore(c);
		return -2;
	}

	hServer->onHttpCallBack = httpCallBack;
	eventLoopCore(c);

	destroyHttpServer(hServer);
	destroyCore(c);
	return 0;
}
